package com.ntd.study.pro.zx.cbpp;

import org.apache.commons.io.IOUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class MsgLoader {

    public String tranCode;

    public MsgLoader(String tranCode) {
        this.tranCode = tranCode;
    }

    public byte[] loadReqMsg() throws UnsupportedEncodingException {
        String headContent = null;
        String bodyContent = null;
        //明文文件流
        FileInputStream fis2 = null;
        Document doc = null;
        try {
            headContent = createHeadContent();

            String msgfilepath = CfgLoader.properties.getProperty("MSGFILEPATH") + tranCode + ".body";
            fis2 = new FileInputStream(msgfilepath);
            bodyContent = IOUtils.toString(fis2, "GBK");

            if ("B0001".equals(tranCode)) {
                bodyContent = encryptFileContent(bodyContent);
            }
            //添加加密和签名节点
            String encryptStr = RSAUtil.encrypt(RSAUtil.getCITICPublicKey(), bodyContent.getBytes("GBK"));
            String signStr = RSAUtil.sign(RSAUtil.getMerPrivateKey(), bodyContent);
            System.out.println("================请求明文为：" + bodyContent);
            System.out.println("================请求明文加密串为：" + encryptStr);
            System.out.println("================请求明文签名串为：" + signStr);
            doc = DocumentHelper.parseText(headContent);
            Utils.putXmlNode(doc, "request.encrypt", encryptStr);
            Utils.putXmlNode(doc, "request.signature", signStr);
            headContent = doc.asXML();
        } catch (FileNotFoundException var1) {
            System.out.println("找不到交易码对应的文件[" + tranCode + ".txt" + "]或明文文件sendmsg.txt");
        } catch (DocumentException var2) {
            var2.printStackTrace();
        } catch (IOException var3) {
            var3.printStackTrace();
        } catch (Exception var4) {
            var4.printStackTrace();
        } finally {
            IOUtils.closeQuietly(fis2);
        }
        System.out.println("================待发送完整请求报文为：" + headContent);
        return headContent.getBytes("GBK");
    }

    public String createHeadContent() {
        Document document = DocumentHelper.createDocument();
        document.setXMLEncoding("GB18030");
        Element root = document.addElement("ROOT");
        Element msgno = root.addElement("msgno").addText(MsgUtil.getMsgNo());
        Element version = root.addElement("version").addText(CfgLoader.properties.getProperty("VERSION"));
        Element trancode = root.addElement("trancode").addText(this.tranCode);
        Element sendNode = root.addElement("sendNode").addText(CfgLoader.properties.getProperty("SENDNODE"));
        Element receiveNode = root.addElement("receiveNode").addText(CfgLoader.properties.getProperty("RECEIVENODE"));
        Element sendDate = root.addElement("sendDate").addText(MsgUtil.getMsgDate());
        Element dateTime = root.addElement("dateTime").addText(MsgUtil.getMsgDateTime());
        return document.asXML();
    }

    public static String encryptFileContent(String xmlStr) throws IOException, DocumentException {
        FileInputStream fis = new FileInputStream(CfgLoader.properties.getProperty("MSGFILEPATH") + "B0001.dtl");
        String dtlContent = IOUtils.toString(fis, "GBK");
        System.out.println("================批量交易明细为：" + dtlContent);
        String zipContent = Utils.zip(dtlContent);
        System.out.println("================明细压缩后：" + zipContent);
        Document doc = DocumentHelper.parseText(xmlStr);
        Utils.putXmlNode(doc, "request.body.fileContent", zipContent);
        return doc.asXML();
    }
}
